package hut.ai.team.actor;

import hut.ai.team.object.QuanCo;

import java.util.ArrayList;
import java.util.Random;

import com.badlogic.gdx.graphics.g2d.TextureRegion;

public class Player {
	public int Phe;
	public QuanCo mQC;
	public ArrayList<QuanCo> dsQuanCo;

	public Player(int x) // Khoi tao cac quan co cho NguoiChoi;
	{
		dsQuanCo = new ArrayList<QuanCo>();
		mQC = new QuanCo();
		this.Phe = x;
		this.KhoiTao();
	}

	protected void KhoiTao() {
		if (this.Phe == 1) {
			mQC = new QuanCo();
			mQC.KhoiTao(0, 4, 1, 2, 1, false);
			dsQuanCo.add(mQC);
			
			mQC = new QuanCo();
			mQC.KhoiTao(0, 3, 2, 1, 1, false);
			dsQuanCo.add(mQC);
			
			mQC = new QuanCo();
			mQC.KhoiTao(0, 5, 2, 1, 1, false);
			dsQuanCo.add(mQC);
			
			mQC = new QuanCo();
			mQC.KhoiTao(0, 2, 3, 1, 1, false);
			dsQuanCo.add(mQC);
			
			mQC = new QuanCo();
			mQC.KhoiTao(0, 6, 3, 1, 1, false);
			dsQuanCo.add(mQC);
			
			mQC = new QuanCo();
			mQC.KhoiTao(0, 0, 4, 1, 1, false);
			dsQuanCo.add(mQC);
			
			mQC = new QuanCo();
			mQC.KhoiTao(0, 8, 4, 1, 1, false);
			dsQuanCo.add(mQC);
			
			mQC = new QuanCo();
			mQC.KhoiTao(2, 1, 5, 1, 1, false);
			dsQuanCo.add(mQC);
			
			mQC = new QuanCo();
			mQC.KhoiTao(2, 7, 5, 1, 1, false);
			dsQuanCo.add(mQC);
			
			mQC = new QuanCo();
			mQC.KhoiTao(0, 1, 6, 1, 1, false);
			dsQuanCo.add(mQC);
			
			mQC = new QuanCo();
			mQC.KhoiTao(0, 7, 6, 1, 1, false);
			dsQuanCo.add(mQC);
		
			mQC = new QuanCo();
			mQC.KhoiTao(3, 0, 7, 1, 1, false);
			dsQuanCo.add(mQC);
			
			mQC = new QuanCo();
			mQC.KhoiTao(3, 2, 7, 1, 1, false);
			dsQuanCo.add(mQC);
			
			mQC = new QuanCo();
			mQC.KhoiTao(3, 4, 7, 1, 1, false);
			dsQuanCo.add(mQC);
			
			mQC = new QuanCo();
			mQC.KhoiTao(3, 6, 7, 1, 1, false);
			dsQuanCo.add(mQC);
			
			mQC = new QuanCo();
			mQC.KhoiTao(3, 8, 7, 1, 1, false);
			dsQuanCo.add(mQC);
			
			shuffleList(dsQuanCo);
			for(int i = 0; i < dsQuanCo.size(); i++){
				dsQuanCo.get(i).setDataOnTable();
			}
		} else {
			mQC = new QuanCo();
			mQC.KhoiTao(9, 4, 1, 2, 2, true);
			dsQuanCo.add(mQC);
			
			mQC = new QuanCo();
			mQC.KhoiTao(9, 3, 2, 1, 2, true);
			dsQuanCo.add(mQC);
			
			mQC = new QuanCo();
			mQC.KhoiTao(9, 5, 2, 1, 2, true);
			dsQuanCo.add(mQC);
			
			mQC = new QuanCo();
			mQC.KhoiTao(9, 2, 3, 1, 2, true);
			dsQuanCo.add(mQC);
			
			mQC = new QuanCo();
			mQC.KhoiTao(9, 6, 3, 1, 2, true);
			dsQuanCo.add(mQC);
			
			mQC = new QuanCo();
			mQC.KhoiTao(9, 0, 4, 1, 2, true);
			dsQuanCo.add(mQC);
			
			mQC = new QuanCo();
			mQC.KhoiTao(9, 8, 4, 1, 2, true);
			dsQuanCo.add(mQC);
			
			mQC = new QuanCo();
			mQC.KhoiTao(7, 1, 5, 1, 2, true);
			dsQuanCo.add(mQC);
			
			mQC = new QuanCo();
			mQC.KhoiTao(7, 7, 5, 1, 2, true);
			dsQuanCo.add(mQC);
			
			mQC = new QuanCo();
			mQC.KhoiTao(9, 1, 6, 1, 2, true);
			dsQuanCo.add(mQC);
			
			mQC = new QuanCo();
			mQC.KhoiTao(9, 7, 6, 1, 2, true);
			dsQuanCo.add(mQC);
			
			mQC = new QuanCo();
			mQC.KhoiTao(6, 0, 7, 1, 2, true);
			dsQuanCo.add(mQC);
			
			mQC = new QuanCo();
			mQC.KhoiTao(6, 2, 7, 1, 2, true);
			dsQuanCo.add(mQC);
			
			mQC = new QuanCo();
			mQC.KhoiTao(6, 4, 7, 1, 2, true);
			dsQuanCo.add(mQC);
			
			mQC = new QuanCo();
			mQC.KhoiTao(6, 6, 7, 1, 2, true);
			dsQuanCo.add(mQC);
			
			mQC = new QuanCo();
			mQC.KhoiTao(6, 8, 7, 1, 2, true);
			dsQuanCo.add(mQC);
			
			shuffleList(dsQuanCo);
			for(int i = 0; i < dsQuanCo.size(); i++){
				dsQuanCo.get(i).setDataOnTable();
			}
			
		}
	}


	public void shuffleList(ArrayList<QuanCo> dsQuanCo) {
		int n = dsQuanCo.size();
		Random random = new Random();
		random.nextInt();
		for (int i = 1; i < n; i++) {
			int change = i + random.nextInt(n - i);
			swap(dsQuanCo, i, change);
		}
	}

	private void swap(ArrayList<QuanCo> dsQuanCo, int i, int change) {
		
		int tempLoai = dsQuanCo.get(i).Loai;
		dsQuanCo.get(i).Loai = dsQuanCo.get(change).Loai; 
		dsQuanCo.get(change).Loai = tempLoai;
		
		TextureRegion temp = dsQuanCo.get(i).mImage;
		dsQuanCo.get(i).mImage = dsQuanCo.get(change).mImage; 
		dsQuanCo.get(change).mImage = temp;
		
		TextureRegion tempSelect = dsQuanCo.get(i).mImageSelect;
		dsQuanCo.get(i).mImageSelect = dsQuanCo.get(change).mImageSelect; 
		dsQuanCo.get(change).mImageSelect = tempSelect;
	}
	
	public void ResetPlayer() {
		dsQuanCo = new ArrayList<QuanCo>();
		mQC = new QuanCo();
	}
	
	public void ResetClick(){
		for(int i = 0; i < dsQuanCo.size(); i++){
			dsQuanCo.get(i).isClick = false;
		}
	}
}
